<template>
  <el-container class="home-container">
    <el-header>
      <i class="el-icon-food food-logo">美食</i>
    </el-header>
    <el-main>
      <el-card>
        <div class="merchant-info" v-for="item in merchantList" :key="item.rid">
          <div class="image-box" @click="toMerchant(item.rid)">
            <el-image
              style="width: 150px; height: 150px"
              :src="imageBaseUrl + item.icon + '.jpg'"></el-image>
          </div>
          <div>
            <el-link :underline="false" style="font-size: 20px" @click="toMerchant(item.rid)">{{item.rnickname}}</el-link>
          </div>
          <div>
            <i class="el-icon-location-outline">{{item.address}}</i>
          </div>
        </div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[6, 12, 24, 30]"
          :page-size="queryInfo.pagesize"
          hide-on-single-page
          layout="total, sizes, prev, pager, next, jumper"
          :total="total" background>
        </el-pagination>
      </el-card>
    </el-main>
  </el-container>
</template>

<script>
export default {
  name: 'Welcome',
  data () {
    return {
      merchantList: [],
      queryInfo: {
        query: '',
        // 当前的页数
        pagenum: 1,
        // 每页显示多少条数据
        pagesize: 12
      },
      total: 0,
      imageBaseUrl: this.$axios.defaults.baseURL + 'static/image/'
    }
  },
  created () {
    this.getMerchantList()
  },
  methods: {
    async getMerchantList () {
      const { data: res } = await this.$axios.get('/user/restaurant/query', { params: this.queryInfo })
      if (res.status !== 2000) {
        return this.$message.error('获取商家信息失败')
      }
      this.merchantList = res.data.restaurants
      this.total = res.data.total
    },
    toMerchant (rid) {
      this.$router.push({ name: 'RestaurantDetails', params: { rid } })
    },
    // 监听每页显示多少条信息
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getFoodList()
    },
    // 监听当前所在页码数
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getFoodList()
    }
  }
}
</script>

<style scoped>
.home-container {
  width: 1200px;
  position: absolute;
  left: 50%;
  transform: translate(-600px);
}
.food-logo {
  font-size: 30px;
  color: #795da3;
  margin-top: 30px;
}
.merchant-info {
  display: inline-block;
  margin-bottom: 30px;
  margin-right: 30px;
}
.image-box {
  cursor: pointer;
}
</style>
